1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.eventbus;
18
19 import com.google.common.collect.Lists;
20
21 import junit.framework.TestCase;
22
23 import java.util.List;
24 import java.util.concurrent.Executor;
25
26
27
28
29
30
31 public class AsyncEventBusTest extends TestCase {
32 private static final String EVENT = "Hello";
33
34
35 private FakeExecutor executor;
36 private AsyncEventBus bus;
37
38 @Override protected void setUp() throws Exception {
39 super.setUp();
40 executor = new FakeExecutor();
41 bus = new AsyncEventBus(executor);
42 }
43
44 public void testBasicDistribution() {
45 StringCatcher catcher = new StringCatcher();
46 bus.register(catcher);
47
48
49 bus.post(EVENT);
50
51 List<String> events = catcher.getEvents();
52 assertTrue("No events should be delivered synchronously.",
53 events.isEmpty());
54
55
56 List<Runnable> tasks = executor.getTasks();
57 assertEquals("One event dispatch task should be queued.", 1, tasks.size());
58
59 tasks.get(0).run();
60
61 assertEquals("One event should be delivered.", 1, events.size());
62 assertEquals("Correct string should be delivered.", EVENT, events.get(0));
63 }
64
65
66
67
68
69
70
71
72 public static class FakeExecutor implements Executor {
73 List<Runnable> tasks = Lists.newArrayList();
74
75 @Override
76 public void execute(Runnable task) {
77 tasks.add(task);
78 }
79
80 public List<Runnable> getTasks() {
81 return tasks;
82 }
83 }
84
85 }